package cn.com.lms.business.examine.web;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.user.persistence.entity.SysUserVo;


import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.util.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.ResponseResult;
import cn.com.lms.business.core.util.SystemUserUtil;

import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import com.bnzj.core.webmvc.util.WebMvcUtils;

import cn.com.lms.business.cozmgt.persistence.entity.CatalogManage;
import cn.com.lms.business.examine.persistence.entity.FbExamineInfo;
import cn.com.lms.business.examine.service.FbExamineInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "问卷调查", description = "问卷调查API")
@RestController
@RequestMapping("/examine")
public class ExamineInfoController {


    @Autowired
    private FbExamineInfoService examineInfoService;

    
    @Operation(summary = "问卷调查列表分页查询", description = "", parameters = {
            @Parameter(name = "fbExamineName", description = "问卷名称进行模糊搜索", schema = @Schema(name = "fbExamineName", description = "", implementation = String.class)),
    })
    @GetMapping(value = "/page")
    public ResponseResult<Page<FbExamineInfo>> getExamineInfo(HttpServletRequest request) {
    	//创建时间开始排序
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "create_time"));
        QueryHelpBean dicQueryBean = examineInfoService.getQueryHelpBean();
        dicQueryBean.setPageable(pageable);
        //问卷名称模糊搜索
        IExpression orCondition1 = CompareExpression.build("d.bnz_examine_name", CompareType.like, request.getParameter("fbExamineName"));
        dicQueryBean.addWhere(orCondition1);
        Page<FbExamineInfo> page = examineInfoService.findAll(dicQueryBean);
        return ResponseResult.success(page);
    }
    
    @Operation(summary = "问卷预览(问卷详细查询)", description = "查询问卷详细内容")
    @GetMapping(value = "/findOne")
    public ResponseResult<List<FbExamineInfo>> findOne(Long id) {
        List<FbExamineInfo> list = examineInfoService.findChild(id);
        return ResponseResult.success(list);
    }
    
    @Operation(summary = "问卷新增", responses = {
            @ApiResponse(responseCode = "1", description = "新增成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseResult.class))),
            @ApiResponse(responseCode = "0", description = "新增失败")})
    @PostMapping(value = "/add")
    public ResponseResult<FbExamineInfo> addExamineInfo(@RequestBody FbExamineInfo examineInfo) {
         SysUserVo currentUser = SystemUserUtil.getCurrentUser();
        EntityUtils.saveBefore(examineInfo, currentUser.getAccount(), currentUser.getCname());
        examineInfoService.save(examineInfo);
        return ResponseResult.success();
    }

    @Operation(summary = "更新", responses = {
            @ApiResponse(responseCode = "1", description = "更新成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseResult.class))),
            @ApiResponse(responseCode = "0", description = "更新失败")})
    @PostMapping("/update")
    public ResponseResult<?> updateExamineInfo(@RequestBody FbExamineInfo examineInfo) {
        if (ObjectUtils.isEmpty(examineInfo.getId())) {
            throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(), "" + ErrorEnum.RECORD_NOT_EXIST.getCode());
        }
         SysUserVo currentUser = SystemUserUtil.getCurrentUser();
        examineInfo.setModifyBy(currentUser.getCname());
        examineInfo.setModifyName(currentUser.getAccount());
        examineInfo.setModifyTime(new Date());
        Optional<FbExamineInfo> examineInfoOpt = examineInfoService.findById(examineInfo.getId());
        if (!examineInfoOpt.isPresent()) {
            throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(), "" + ErrorEnum.RECORD_NOT_EXIST.getCode());
        }
        FbExamineInfo examineInfoDb = examineInfoOpt.get();
        BeanUtils.copyProperties(examineInfo, examineInfoDb);
        examineInfoService.save(examineInfoDb);
        return ResponseResult.success();
    }


}
